package com.Liuhang.Dao;

import com.Liuhang.JDBC.util;
import com.Liuhang.entity.user;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class emp_Search extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("已进入方法");
        if(util.isNull(req.getParameter("name"))){
            int id = Integer.parseInt(req.getParameter("ID"));
            List u1 = emp_seach(id);
            req.setAttribute("list",u1);
            System.out.println(u1);
        }
        System.out.println("seach方法已结束！");
        ServletContext context = this.getServletContext();
        context.getRequestDispatcher("/seachResult.jsp").forward(req,resp);
    }

    public List emp_seach(int id){

        List list = new ArrayList();
        String sql ="select * from t_emp t,t_dep d WHERE t.dep_id=d.pid and t.id= "+id+"";
        try {
            Statement st = util.getStatement();
            ResultSet rs =  st.executeQuery(sql);
            while (rs.next()){
                user u1 ;
                u1 = new user(rs.getInt(1),rs.getString(    "name"),rs.getString("sex"),rs.getInt("phone"),rs.getInt("dep_id"),rs.getInt("money"),rs.getString("dep_name"),rs.getString("passwd"));
                list.add(u1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return list;
    }

    public List emp_seach(String name){
        ResultSet rs;
        List list = null;
        String sql ="select * from t_emp t,t_dep d WHERE t.dep_id=d.pid and t.name = "+name+"";
        try {
            Statement st = util.getStatement();
            rs = st.executeQuery(sql);
            while (rs.next()){
                user u1 = new user(rs.getInt(1),rs.getString("name"),rs.getString("sex"),rs.getInt("phone"),rs.getInt("dep_id"),rs.getInt("money"),rs.getString("dep_name"),rs.getString("passwd"));
                list.add(u1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return list;
    }

    public List emp_seachMoney(int money){
        ResultSet rs;
        List list = null;
        String sql ="select * from t_emp t,t_dep d WHERE t.dep_id=d.pid and t.money = "+money+"";
        try {
            Statement st = util.getStatement();
            rs = st.executeQuery(sql);
            while (rs.next()){
                user u1 = new user(rs.getInt(1),rs.getString("name"),rs.getString("sex"),rs.getInt("phone"),rs.getInt("dep_id"),rs.getInt("money"),rs.getString("dep_name"),rs.getString("passwd"));
                list.add(u1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return list;
    }
}
